<?php
/*
 * 公共类库
 * @author changshuaiqiang
 */
namespace App\Libs;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Base{

    /**
     * 重写实现 http_build_query 提交实现(同名key)key=val1&key=val2
     * @param array $formData 数据数组
     * @param string $numericPrefix 数字索引时附加的Key前缀
     * @param string $argSeparator 参数分隔符(默认为&)
     * @param string $prefixKey Key 数组参数，实现同名方式调用接口
     * @return string
     */
    private function build_query($formData, $numericPrefix = '', $argSeparator = '&', $prefixKey = '')
    {
        $str = '';
        foreach ($formData as $key => $val) {
            if (!is_array($val)) {
                $str .= $argSeparator;
                if ($prefixKey === '') {
                    if (is_int($key)) {
                        $str .= $numericPrefix;
                    }
                    $str .= urlencode($key) . '=' . urlencode($val);
                } else {
                    $str .= urlencode($prefixKey) . '=' . urlencode($val);
                }
            } else {
                if ($prefixKey == '') {
                    $prefixKey .= $key;
                }
                if (is_array($val[0])) {
                    $arr = array();
                    $arr[$key] = $val[0];
                    $str .= $argSeparator . http_build_query($arr);
                } else {
                    $str .= $argSeparator . $this->build_query($val, $numericPrefix, $argSeparator, $prefixKey);
                }
                $prefixKey = '';
            }
        }
        return substr($str, strlen($argSeparator));
    }
    /**
     * 发送请求
     * @param  [type] $url        url 地址
     * @param  [type] $data       提交的数据
     * @param  array  $httpHeader head 头
     * @return [type]             返回结果，false 失败
     */
    public static function postCurl($url, $data, $httpHeader = array())
    {
        $ch = curl_init();
        $strReg = '/application\/json/i';
        if (count($httpHeader) > 0 && preg_match($strReg, $httpHeader[0])) {
            $data = json_encode($data);
        } else {
            $data = http_build_query($data);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2");

        $strResult = curl_exec($ch);
        $rInfo = curl_getinfo($ch);
        curl_close($ch);
        if ($rInfo['http_code'] == '200') {
            $arrResult = json_decode($strResult, true);
        } else {
            return false;
        }
        return $arrResult;
    }

    /**
     * 上传图片到七牛服务器
     * @param  string $path 文件路径
     * @param  string $name 上传文件名
     * @return [type]       [description]
     */
    public static function uploadImg($filePath, $name)
    {
        $bucket = config('app.qiniu_bucket');
        $accessKey = config('app.qiniu_access');
        $secretKey = config('app.qiniu_secret');
        $auth = new Auth($accessKey, $secretKey);
        $upToken = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传。
        $uploadMgr = new UploadManager();
        $res = $uploadMgr->putFile($upToken, $name, $filePath);
        return $res;
    }
}